<?php
class LoginTest extends MasterTest {

    private $test = true ;

    private $lv ;
    private $lm ;
    private $lc ;

    public function __construct() {
        parent::__construct() ;     // We have to make sure that the parent-construct is ran.
        require_once("../Controllers/LoginController.php") ;

        require_once("../Views/LoginView.php") ;
        require_once("../Models/LoginModel.php") ;
        
        $this->lm = new LoginModel() ;
        $this->lv = new LoginView($this->lm) ;
        $this->lc = new LoginController($this->lv, $this->lm) ;
    }

    public function Tests() {

        if(!$this->LoginModelTests()) {
            $this->test = false ;
        }
        if(!$this->LoginViewTests()) {
            $this->test = false ;
        }
        if(!$this->LoginControllerTests()) {
            $this->test = false ;
        }


        return $this->test ;
    }

    
    private function LoginModelTests() {


        $retBool = true ;

   	// Start by logging out, so we have a fresh start
   	if($this->lm->doLogout() !== true) {
   	    array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginModel", "function"=>"DoLogout()", "msg"=>"DoLogout test failed"));
   	    $retBool = false ;
   	}
   	
   	// Now no-one should be logged in, we test that
   	if($this->lm->isLoggedIn() !== false) {
   	    array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginModel", "function"=>"IsLoggedIn()", "msg"=>"IsLoggedIn test failed"));
   	    $retBool = false ;
   	}

   	// We try to login with incorrect username and password
   	if($this->lm->doLogin("notCorrect", "nietherThis") !== false) {
   	    array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginModel", "function"=>"DoLogin()", "msg"=>"DoLogin test failed when trying wrong username and password"));
   	    $retBool = false ;
   	}

   	// And we try with correct username and password
   	if($this->lm->doLogin("user", md5("password")) !== true) {
   	    array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginModel", "function"=>"DoLogin()", "msg"=>"DoLogin test failed with correct username and password"));
   	    $retBool = false ;
   	}

   	// Now we should be logged in, so it is a good time to test IsLoggedIn again
   	if($this->lm->isLoggedIn() !== true) {
   	    array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginModel", "function"=>"IsLoggedIn()", "msg"=>"IsLoggedIn test failed when logged in"));
   	    $retBool = false ;
   	}

   	// Lets try DoLogout again now that we know that someone is logged in.
   	if($this->lm->doLogout() !== true) {
   	    array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginModel", "function"=>"DoLogout()", "msg"=>"DoLogout test failed when logged in"));
   	    $retBool = false ;
   	}

   	// And try to login again with correct username but wrong password
   	if($this->lm->doLogin("user", "wrongPassword") !== false) {
   	    array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginModel", "function"=>"DoLogin()", "msg"=>"DoLogin test failed when using correct username and wrong password"));
   	    $retBool = false ;
   	}

        // Test if username exists, incorrect username 
        if($this->lm->checkUsername("noUser") !== false) {
            array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginModel", "function"=>"checkUsername()", "msg"=>"CheckUsername test failed when checking wrong username")) ;
            $retBool = false ;        
        }
        
        // Test if username exists, correct username
        if($this->lm->checkUsername("user") !== true) {
            array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginModel", "function"=>"checkUsername()", "msg"=>"CheckUsername test failed when checking correct username")) ;
            $retBool = false ;        
        }

        return $retBool ;
    }

    private function LoginControllerTests() {

        $retBool = true ;

        // Check output when not logged in

        // Make sure noone is logged in

        $this->lm->doLogout() ;

        $expOutput = "Du är inte inloggad" ;

        if(!strpos($this->lc->doControll(), $expOutput)) {
          
            array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginController", "function"=>"doControll()", "msg"=>"Something went wrong in the ControllerTest")) ;
            $retBool = false ;
        }

        // Log-in
        $this->lm->doLogin("user", "5f4dcc3b5aa765d61d8327deb882cf99") ;

        $expOutput = "Du är inloggad" ;

        if(!strpos($this->lc->doControll(), $expOutput)) {
          
            array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginController", "function"=>"doControll()", "msg"=>"Something went wrong in the ControllerTest")) ;
            $retBool = false ;
        }

        return $retBool ; 
    }

    public function LoginViewTests() {
        $retBool = true ;

        // First we check so that cookie-test is not set
        if($this->lv->doUnsetCookie("cookie-test") !== true) {
            array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginView", "function"=>"doUnsetCookie()", "msg"=>"Something went wrong when trying to unset cookie")) ;
            $retBool = false ;
        }

        // check so that no cookie is set
        if($this->lv->isCookieSet("cookie-test") !== false) {
            array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginView", "function"=>"isCookieSet()", "msg"=>"The cookie didn't unset properly.")) ;
            $retBool = false ;

        }

        // And check so we don't get a cookie from getCookie
        if($this->lv->getCookie("cookie-test") !== null) {
            array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginView", "function"=>"getCookie()", "msg"=>"The cookie didn't unset properly.")) ;
            $retBool = false ;

        }

        // Set the test-cookie
        if($this->lv->doSetCookie("testar", "cookie-test")  !== true) {
            $retBool = false ;
        }

        // check so that cookie is set
        if($this->lv->isCookieSet("cookie-test") !== true) {
            array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginView", "function"=>"isCookieSet()", "msg"=>"The cookie didn't set properly.")) ;
            $retBool = false ;

        }

        // And check so we the expected content
        if($this->lv->getCookie("cookie-test") !== "testar") {
            array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginView", "function"=>"getCookie()", "msg"=>"The cookie didn't set properly, nuexpected content.")) ;
            $retBool = false ;

        }

        // And unset the set cookie
        if($this->lv->doUnsetCookie("cookie-test") !== true) {
            array_push($this->errors, array("line"=>__LINE__, "class"=>"LoginController", "function"=>"doControll()", "msg"=>"Something went wrong in the ControllerTest")) ;
            $retBool = false ;
        }


        return $retBool ;


    }

}
